#include "characterclasstablemodel.h"
#include "classlevel.h"


#include <QStringList>
#include <QDebug>


CharacterClassTableModel::CharacterClassTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

void CharacterClassTableModel::setCharacterClass(CharacterClass *root)
{
//    if (m_root.data() == root)
//    {
//        return;
//    }

    emit layoutAboutToBeChanged();
    beginResetModel();

    m_root = root;

    endResetModel();
    emit layoutChanged();
}

int CharacterClassTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    if (m_root.isNull())
    {
        return 0;
    }

    return m_root->findChildren<ClassLevel*>().count();
}

int CharacterClassTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    //    if (m_root.isNull())
//    {
//        return 0;
//    }

    return (Column_LAST - Column_FIRST + 1);
}

QVariant CharacterClassTableModel::data(const QModelIndex &index, int role) const
{
    if (m_root.isNull() || !index.isValid() || (role != Qt::DisplayRole && role != Qt::TextAlignmentRole))
    {
        return QVariant();
    }

    if (role == Qt::TextAlignmentRole && index.column() != Column_SPEC)
    {
        return Qt::AlignCenter;
    }

    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }



    int row = index.row();
    if (row < 0)
    {
        return QVariant();
    }

    QList<ClassLevel*> levels = m_root->findChildren<ClassLevel*>();
    if (row >= levels.count())
    {
        return QVariant();
    }

    ClassLevel* level = levels.at(row);

    switch (index.column())
    {
    case Column_LEVEL:
        return level->level();

    case Column_BA:
        return level->baseAttackValue();

    case Column_FORT:
        return level->fortSaveValue();

    case Column_REF:
        return level->refSaveValue();

    case Column_WILL:
        return level->willSaveValue();

    case Column_SPEC:
    {
        QStringList list;
        foreach (SpecialAbility* spec, level->findChildren<SpecialAbility*>(QString(), Qt::FindDirectChildrenOnly))
        {
            list << spec->name();
        }

        qSort(list);
        return list.join(", ");
    }
    default:
        break;
    }

    return QVariant();
}

QVariant CharacterClassTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    if (orientation == Qt::Vertical)
    {
        return section+1;
    }

    switch (section)
    {
    case Column_LEVEL:
        return tr("Level");

    case Column_BA:
        return tr("BA");

    case Column_FORT:
        return tr("Fort");

    case Column_REF:
        return tr("Ref");

    case Column_WILL:
        return tr("Will");

    case Column_SPEC:
    {
        return tr("Special");
    }
    default:
        break;
    }

    return QVariant();
}

void CharacterClassTableModel::revert()
{
    qDebug() << Q_FUNC_INFO;

    emit layoutAboutToBeChanged();

    beginResetModel();

    endResetModel();

    emit layoutChanged();
}
